package com.aixx.springcloud.alibaba.service.impl;

import com.aixx.springcloud.alibaba.dao.OrderDao;
import com.aixx.springcloud.alibaba.domain.Order;
import com.aixx.springcloud.alibaba.service.AccountService;
import com.aixx.springcloud.alibaba.service.OrderService;
import com.aixx.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author shkstart
 * @create 2021-02-18 0:22
 */
@Service
@Slf4j
public class OrderServiceimpl implements OrderService {
     @Resource
     private OrderDao orderDao;

    @Resource
    private StorageService storageService;//库存


     @Resource
     private AccountService accountService; //账户

    @Override
    //只需要在service业务类的方法上加上该注解，name值自定义唯一即可。
    @GlobalTransactional(name = "dkf-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("--------》 开始创建订单");
      orderDao.create(order);

        log.info("库存减少");
      storageService.decrease(order.getProductId(),order.getCount());
        log.info("库存减少完成");

       log.info("账户减少");
       accountService.decrease(order.getUserId(),order.getMoney());
        log.info("账户减少完成");

        log.info("订单状态");
        orderDao.update(order.getUserId(),0);
        log.info("订单结束");
    }
}
